using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.UserDomain;
using ShivamEMR.Model.DataInterfaces.UsersDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;

namespace ShivamEMR.Web.Areas.User.Controllers
{   
    public class UserRightGroupController : Controller
    {
		private readonly IUserRightGroupRepository userrightgroupRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public UserRightGroupController()  
        {
            userrightgroupRepository = Registry.RepositoryFactory.GetUserRightGroupRepository();
        }

        public UserRightGroupController(IUserRightGroupRepository userrightgroupRepository)
        {
			this.userrightgroupRepository = userrightgroupRepository;
        }

        //
        // GET: /UserRightGroup/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index()
        {
            return View(userrightgroupRepository.All);//Including(userrightgroup => userrightgroup.Parent, userrightgroup => userrightgroup.Children, userrightgroup => userrightgroup.Rights));
        }

        //
        // GET: /UserRightGroup/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(int id)
        {
            return View(userrightgroupRepository.Find(id));
        }

        //
        // GET: /UserRightGroup/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
			ViewBag.PossibleParents = userrightgroupRepository.All;
            return View();
        } 

        //
        // POST: /UserRightGroup/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(UserRightGroup userrightgroup)
        {
            if (ModelState.IsValid) {
                userrightgroupRepository.Create(userrightgroup);
                userrightgroupRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleParents = userrightgroupRepository.All;
				return View();
			}
        }
        
        //
        // GET: /UserRightGroup/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleParents = userrightgroupRepository.All;
             return View(userrightgroupRepository.Find(id));
        }

        //
        // POST: /UserRightGroup/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(UserRightGroup userrightgroup)
        {
            if (ModelState.IsValid) {
                userrightgroupRepository.Update(userrightgroup);
                userrightgroupRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleParents = userrightgroupRepository.All;
				return View();
			}
        }

        //
        // GET: /UserRightGroup/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(userrightgroupRepository.Find(id));
        }

        //
        // POST: /UserRightGroup/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var rg = userrightgroupRepository.Find(id);
            userrightgroupRepository.Delete(rg);
            userrightgroupRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

